﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
using System.Data;
using System.Windows;
using WPFExcelReport;
namespace INTEX.SCHEDULER
{
    /// <summary>
    /// Interaction logic for Machine.xaml
    /// </summary>

    public partial class Step : WPFExcelReport.ViewBase
    {
        StepViewModel vm;
        public DataRow dwABFOLGE
        {
            get
            {
                return vm.dwABFOLGE;
            }
            set
            {
                vm.dwABFOLGE = value;
            }
        }
        public DataTable KSLEIST
        {
            get
            {
                return vm.KSLEIST;
            }
            set
            {
                vm.KSLEIST = value;
            }
        }
        public Step()
        {
            InitializeComponent();
            vm = new StepViewModel();
            this.DataContext = vm;
        }
        public void SetUp()
        {
            if (dwABFOLGE == null)
                return;
            // //STEP,OPERATION,QTYACT,MACHINEID,DAILYQTY
            vm.STEP = dwABFOLGE["ABFAFOLGE"].ToString();
            vm.OPERATION = Helper.GetGB2312Value(dwABFOLGE["ABFBZAGANG"].ToString()).Trim();
            vm.QTYACT = Convert.ToDouble(dwABFOLGE["ABFIMG"].ToString()) / 1000;
            vm.MACHINEID = dwABFOLGE["ABFIMPOS"].ToString();
            vm.DAILYQTY = Convert.ToDouble(dwABFOLGE["ABFIMG"].ToString()) / 1000;
            //KSLMGKG,KSLMDAT,MACHINEID
            Dictionary<string, double> DAILY = new Dictionary<string, double>();
            vm.MNUM = 0;
            vm.DAILY = "";
            if (KSLEIST == null)
                return;
            foreach (DataRow dwKSLEIST in KSLEIST.Rows)
            {
                vm.MNUM++;

                string mid = dwKSLEIST["MACHINEID"].ToString();
                if (mid.Trim().Length == 0)
                {
                    mid = dwKSLEIST["KSLSMPOS"].ToString();
                }
                vm.MACHINEID = mid;
                string KSLMDAT = dwKSLEIST["KSLMDAT"].ToString();
                double KSLMGKG = Helper.RoundX(Convert.ToDouble(dwKSLEIST["KSLMGKG"].ToString()),1);
                if (DAILY.ContainsKey(KSLMDAT))
                {
                    DAILY[KSLMDAT] = DAILY[KSLMDAT] + KSLMGKG;
                }
                else
                {
                    DAILY.Add(KSLMDAT, KSLMGKG);
                }
            }
            double total = 0;
            int days = 0;
            foreach (KeyValuePair<string, double> kp in DAILY)
            {
                days++;
                string date = kp.Key.Substring(0, 6);
                string day = Convert.ToInt16(kp.Key.Substring(4, 2)).ToString();
                double qty = kp.Value;
                total += qty;
                vm.DAILY += day + ":" + qty + ",";
            }
            vm.DAILY += "***";
            vm.DAILY = vm.DAILY.Replace(",***", "");
            vm.DAILYQTY = Helper.RoundX(total / days, 1);
        }
    }
}
